package com.magold.game.hud;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.hud.HUD;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.shape.IAreaShape;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.sprite.ButtonSprite.OnClickListener;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.modifier.ease.EaseCubicIn;
import org.andengine.util.modifier.ease.EaseCubicOut;
import org.andengine.util.modifier.ease.EaseLinear;

import com.magold.game.MainGameActivity;
import com.magold.game.level.LevelIntro;
import com.magold.game.level.RecordHandle;
import com.magold.logic.magold.Magold;
import com.magold.modifier.ModifierFactory;
import com.magold.texture.TextureFactory;
import com.magold.time.AccurateTime;

public class GameStatusScene extends HUD {
	private final GameScore mScore;
	private final GameTimer mTimer;
	private final PowerGauge mPowerGauge;
	private final SpeedDisplayer mSpeedDisplayer;
	
	public GameStatusScene(VertexBufferObjectManager pVBOM, Magold pStatusProvider, Scene pGameScene, Camera pCamera, final MainGameActivity pContext) {
		mScore = new GameScore();
		mScore.attachToScene(800, 0, this);
		pStatusProvider.addScoreListener(mScore);
		
		mPowerGauge = new PowerGauge(pStatusProvider, pVBOM);
		mPowerGauge.attachToScene(0, 230, this);
		pGameScene.registerUpdateHandler(mPowerGauge);
		
		IAreaShape actor = pStatusProvider.getSprite();
		mSpeedDisplayer = new SpeedDisplayer(EaseLinear.getInstance(), pStatusProvider, actor, pCamera, pVBOM);
		mSpeedDisplayer.attachToScene(326, 383, this);
		pGameScene.registerUpdateHandler(mSpeedDisplayer);
		
		String path = pContext.getIntent().getStringExtra(LevelIntro.ASSET_PATH_KEY);
		RecordHandle record = new RecordHandle(path, pContext);
		mTimer = new GameTimer(new AccurateTime(record.getMinTime().parseMilliseconds()));
		mTimer.attachToScene(350, 0, this);
		pGameScene.registerUpdateHandler(mTimer);
		
		ITiledTextureRegion tile =  TextureFactory.createTiledAnim("hud/restart_button.png", 1, 2);
		OnClickListener clickListener = new OnClickListener() {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX,
					float pTouchAreaLocalY) {
				pContext.getSoundByName("menu.WAV").play();
				pContext.restart();
			}
		};
		ButtonSprite restart = new ButtonSprite(-40, -42, tile, pVBOM, clickListener );
		restart.setScale(0.5f);
		restart.setAlpha(0.3f);
		IEntityModifier scaleLoop = ModifierFactory.createPlaybackScaleModifier(1, 0.3f, 0.4f, EaseCubicOut.getInstance(), EaseCubicIn.getInstance());
		restart.registerEntityModifier(scaleLoop);
		this.attachChild(restart);
		this.registerTouchArea(restart);
	}
	
}
